package com.adjy.pathbuilders.screens;

import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.adjy.pathbuilders.objects.CoinMove;
import com.adjy.pathbuilders.objects.GameEntityType;
import com.adjy.pathbuilders.rendering.AnimationPack;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.world.GameWorld;

public class AnimationCoins implements GameScreen{
	
	static CoinMove coins[];
	static final int MAX_COINS = 30;
	private AnimationPack coinPack;
	private GameWorld parent;
	private AnimationFactory animationFactory;
	private Paint defaultPaint;
	
	public AnimationCoins(AnimationFactory anFactory, GameWorld parent) {
		this.parent = parent;
		this.animationFactory = anFactory;
		createDefaultPaint();
		clearData();
	}
	
	public void clearData(){
		coins = new CoinMove[MAX_COINS + 1];
	}
	
	private void createDefaultPaint() {
		this.defaultPaint = new Paint();
		this.defaultPaint.setColor(Color.BLUE);
		this.defaultPaint.setTextSize(50);
		this.defaultPaint.setAntiAlias(true);
	}

	@Override
	public void load() {
		this.coinPack = this.animationFactory.getAnimationsFor(GameEntityType.COIN); // 50, 600
		
	}

	@Override
	public void release() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void input(MotionEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render(GameCanvas mcanvas, float timeElapsed) {
		
		for(int i = 0 ; i < coins.length ; i++){
			CoinMove coin = coins[i];
			
			if(coin != null && coin.isMoving()){
//				mcanvas.drawTextWithConversion("("+String.valueOf(coin.getX())+","+
//			String.valueOf(coin.getY())+")", 200, 70, this.defaultPaint);
				
				
				
				
				
//				mcanvas.drawTextWithConversion("("+coin.getDivX()+","+
//						String.valueOf(coin.getDivY())+")", 200, 300, this.defaultPaint);
				
				if(coin.getX() > ScoreScreen.POS_X_COIN+2 ){
					
					coin.setY((int)(coin.getY()+coin.getDivY()));
					
					coin.setX((int)(coin.getX()-coin.getDivX()));
					
				}
				else{
					coins[i].kill();
					coins[i] = null;
				}
				//else if(coin.getY() < ScoreScreen.POS_Y_COIN+15)
				//	coin.setY((int)(coin.getY()+divY));
				//else
				//	coins[i] = null;
					
				
				
				if(coins[i] != null){
				mcanvas.drawBitmapFromPhysics(this.coinPack.getAnimation(
						GameEntityType.getDefaultAnimationName(GameEntityType.COIN)).getCurrentFrame(timeElapsed),
						coin.getX(), coin.getY());
				}
					
			}
		}
	}

	@Override
	public void step(float timePassed) {
		// TODO Auto-generated method stub
		
	}
		
	public static void moveCoin(int ID){
		
		for(CoinMove coin : coins){
			if(coin != null && coin.getID() == ID){
				coin.setMoving(true);
				break;
			}
		}
	}
	
	public boolean createCoin(int posX, int posY, AnimationPack ap){
		CoinMove coin = new CoinMove(posX, posY, ap);
		double divX = Math.abs(ScoreScreen.POS_X_COIN - posX)/12.0;
		double divY = Math.abs(ScoreScreen.POS_Y_COIN+10 - posY)/12.0;
		
		coin.setDivX(divX);
		coin.setDivY(divY);
		
		for(int i = 0; i < coins.length; i++)
			if(coins[i] == null){
				
				coins[i] = coin;				
				return true;
			}
		
		return false;
		
	}

}
